
Public Class frmUtilityRegistrySettings
    Public mblnCalledInner As Boolean

    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Me.Close()
        If mblnCalledInner = False Then End
    End Sub

    Private Sub frmUtilityRegistrySettings_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        Me.Dispose()
    End Sub

    Private Sub frmUtilityRegistrySettings_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        gstrDatabase = ""

        If cboServerName.Items.Count > 0 Then
            cboServerName.SelectedIndex = 0
            cboServerName.Focus()
        End If

    End Sub

    Public Sub unDefineServer()
        SetRegistryValue("Defined", "0")
    End Sub

    Private Sub cboServerName_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs)
        'cboServerName.SelectAll()
    End Sub

    Private Sub txtLoginName_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtLoginName.GotFocus
        txtLoginName.SelectAll()
    End Sub

    Private Sub txtPassword_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtPassword.GotFocus
        txtPassword.SelectAll()
    End Sub

    Private Sub btnOk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOk.Click
        Dim blnsuceeded As Boolean

        blnsuceeded = defineServerLocation(Trim(cboServerName.Text), TEncrypt(Trim(txtPassword.Text)), Trim(txtLoginName.Text))
        If blnsuceeded = False Then
            MsgBox("Could not write server location to the registry", vbCritical + vbOKOnly, "Registry Problem")
            unDefineServer()
            End
        End If
        Me.Close()
    End Sub

    Public Function defineServerLocation(ByVal DS As String, ByVal PWD As String, ByVal uID As String) As Boolean
        Try
            SetRegistryValue("Defined", 1)
            SetRegistryValue("Authentication", IIf(chkAuth.Checked = True, 1, 0))
            SetRegistryValue("Data Source", DS)
            SetRegistryValue("Network Data Source", DS)
            SetRegistryValue("Persist Security Info", "False")
            SetRegistryValue("Provider", "SQLOLEDB.1")
            SetRegistryValue("User ID", uID)
            SetRegistryValue("PWD", PWD)
            SetRegistryValue("Backup Path", "")
            defineServerLocation = True

            blnGetPWD()        'Sets the value for the global password for reports
            blnGetUID()        'Sets the value for the global user id

        Catch ex As Exception
            defineServerLocation = False
            MessageBox.Show(ex.Message, gstrMessage, MessageBoxButtons.OK, MessageBoxIcon.Error)
            AddErrorLog("Error " & Err.Number & " (" & Err.Description & ") in Procedure " & _
                Reflection.MethodBase.GetCurrentMethod.Name & " Of " & Me.Name)
        End Try
    End Function

    Private Sub chkAuth_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkAuth.CheckedChanged
        If chkAuth.Checked = True Then
            txtLoginName.Text = "" : txtLoginName.Enabled = False
            txtPassword.Text = "" : txtPassword.Enabled = False
        Else
            txtLoginName.Enabled = True : txtPassword.Enabled = True
        End If
    End Sub
End Class